/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
               
Revisions:

Author        Date    Description:

*/
using System;
using System.Threading;


namespace Cafechess.Threading
{
  /// <summary>
  /// BoundSemaphore implements a counting semaphore with a boundary for the number
  /// of calls to <code>Release()</code>, before a thread is woken up.
  /// </summary>
  public class BoundSemaphore : AbstractSemaphore
  {
    /// <summary>
    /// Default constructor.  Initializes the semaphore with a minimum and maximum
    /// acquisition count.   Thus, a thread will block when the number of calls to
    /// <code>Aquire()</code> exceed the maximum value.  A thread is woken up when the
    /// number of calls to <code>Release()</code> is lower than the minimum value.
    /// </summary>
    /// 
    /// <param name="nMinAcquisitionCount">The required acquisition count before a call to <code>Release()</code> signals.</param>
    /// <param name="nMaxAcquisitionCount">The required acquisition count before a call to <code>Aquire()</code> blocks.</param>
    public BoundSemaphore( int nMinAcquisitionCount, int nMaxAcquisitionCount ) : base( nMinAcquisitionCount, nMaxAcquisitionCount )
    {
    }

    /// <summary>
    /// Resets the semaphore's acquisition count and resumes all blocked threads.
    /// </summary>
    public override void Reset()
    {
      lock( this ) 
      {
        cnCount = 0;
        Monitor.PulseAll( this );
      }
    }
  }
}
